//
// Created by liushuai on 2020/4/3.
//
#include "lengthOfLongestSubstring.h"
int lengthOfLongestSubstring(string s){
    map<char, int> char_dict;
    int start = 0;
    int max_num = 0;
    int size = s.size();
    for(int end=0;end<size;end++){
        char current_char = s.at(end);
        if(!char_dict.count(current_char))//不存在字符，更新索引
            char_dict[current_char]=end;
        else{
            start = max(char_dict[current_char]+1,start); // 存在字符，起点为重复字符索引的下一位，防止重复出现的输出的下一位比当前的start小
            char_dict[current_char] = end;
        }

        max_num = std::max(end-start+1,max_num);
    }
    return max_num;
}
